//获取应用实例
var app = getApp();
var reLoginNum = 0;
var timer = null;
var lock = false;

// 初始化数据
var init = {

  // 数据
  data: {
    // 是否全选
    all: true,

    edits: false,
  },

  // 页面第一次加载
  onLoad: function (options) {

    this.setData({
      options: options
    })

  },
  // 每次进来都执行
  onShow: function () {
    var that = this;
    // 登录授权
    app.getLoginSession(function (session) {
      that.getData(session);
    })
  },
  // 获取数据
  getData: function (session) {

    var that = this;

    // 请求前
    that.setData({

      // 开启loading
      loading: true
    });
    // 请求
    wx.request({

      // 请求地址
      url: app.api,

      // 请求参数
      data: {
        app: 'member',
        act: 'myCollect',
        t_session: session
      },

      // 请求成功
      success: function (res) {
        var data = res.data;
        
        if (data.done && data.retval.length != 0) {
          that.setData({
            data: that.index(data.retval)
          })
          console.log(that.index(data.retval));
          for (var i = 0; i < that.data.data.length; i++) {
            that.data.data[i].type_id = true;
          }
          

          that.setData({
            loading: false,
            data:that.data.data,
            totalAmount: data.retval.total_price,
            recId: data.rec
          })
          console.log(that.data.data);

        } else {
          that.setData({
            errorTips: '暂无收藏'
          })
        }
      },

      // 请求失败
      fail: function () {
        that.setData({
          errorTips: '接口请求失败，app：cart'
        })
      }
    });

  },

  // 或者最终结果
  getSelect: function () {
    var data = that.data.data.goods;

  },

  // 单选选择
  selectOne: function (event) {
    var that = this;
    var id = event.currentTarget.dataset.id;
    var data = that.data.data;
    console.log(data.length);
    for (var i = 0; i < data.length; i++) {
      if (id == data[i].id) {
        data[i].type_id = !data[i].type_id
      }
    }
    that.setData({
      data: data
    })
  },
  // 全选反选
  selectAll: function () {
    var that = this;
    var data = this.data.data;
    var status = that.data.all ? false : true;
    for (var i = 0; i < data.length - 1; i++) {
      // console.log(data.length);
      data[i].type_id = status;
    }
    that.setData({
      data: data,
      all: !that.data.all,
    })
  },
  // 结算
  remove: function () {
    var that = this;
    var data = that.data.data;
    console.log(data);
    var arr = [];
    for (var i = 0; i < data.length; i++) {
      if (data[i].type_id) {
        arr.push(data[i].id);
      }
    }
    arr = arr.join(",");//数组转字符串
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'member',
          act: 'dropCollect',
          id: arr,
          t_session: session
        },
        success: function (res) {
          var data = res.data;
          if (data.done) {
            that.getData(session);
          } else {
            wx.showToast({
              title: '删除收藏失败',
              icon: 'loading'
            })
          }
        },
        fail: function () {
          wx.showToast({
            title: '接口请求失败',
            icon: 'loading'
          })
        }
      })
    })
  },// 编辑
  edit: function () {
    var that = this;
    if (that.data.edits == true) {
      that.setData({
        edits: false
      })
    } else {
      that.setData({
        edits: true
      })
    }
  },
  index: function (list) {//对下标重新赋值下标从0开始
    var arr = [];
    for (var i in list) {
      var str = list[i]
      arr.push(str);
    }
    list = arr;
    return list
  }

};

// 注册页面
Page(init);
